﻿#include <csignal>
#include "pelco_d_server.hpp"

std::shared_ptr<PelcoDSmoothServer> g_server;
std::atomic<bool> g_stop{ false };

void signal_handler(int)
{
    g_stop = true;
    if (g_server) g_server->stop();
}

int main() {
    unsigned short port = 0;
    std::cout << "请输入 Pelco-D 服务端口 (1024-65535): ";
    while (!(std::cin >> port) || port < 1024)
    {
        std::cin.clear();
        std::cin.ignore(1024, '\n');
        std::cout << "输入无效，请重新输入端口 (1024-65535): ";
    }
    g_server = std::make_shared<PelcoDSmoothServer>(port);

    std::signal(SIGINT, signal_handler);
    std::signal(SIGTERM, signal_handler);

    std::thread srv_thread([port]()
        {
            std::cout << "Pelco-D 服务监听端口 :" << port << std::endl;
            g_server->run();
        });

    while (!g_stop && srv_thread.joinable())
        srv_thread.join();

    return 0;
}
